package com.homestead.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.homestead.bo.CreateOrgBo;
import com.homestead.constant.Constant;
import com.homestead.enums.OrgTypeEnum;
import com.homestead.domain.entity.OrgInfo;
import com.homestead.domain.mapper.OrgInfoMapper;
import com.homestead.dto.OrgPathDo;
import com.homestead.enums.OrgInfoErrorCode;
import com.homestead.exception.OrgInfoException;
import com.homestead.service.OrgInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author HanBin_Yang
 * @since 2021/4/10 18:35
 */
@Service
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class OrgInfoServiceImpl extends ServiceImpl<OrgInfoMapper, OrgInfo> implements OrgInfoService {
    @Override
    public void create(CreateOrgBo bo) {
        // 非平台 parentCode必填
        if (!OrgTypeEnum.PLATFORM.getOrgLevel().equals(bo.getOrgLevel()) && StringUtils.isBlank(bo.getParentCode())) {
            throw new OrgInfoException(OrgInfoErrorCode.PARENT_ORG_CODE_NULL);
        }

        OrgInfo orgInfo = new OrgInfo();
        if (!OrgTypeEnum.PLATFORM.getOrgLevel().equals(bo.getOrgLevel())) {
            OrgInfo orgInfoPo = new LambdaQueryChainWrapper<>(baseMapper).eq(OrgInfo::getOrgCode, bo.getParentCode()).one();
            if (Objects.isNull(orgInfoPo)) {
                throw new OrgInfoException(OrgInfoErrorCode.PARENT_ORG_NOT_EXIST);
            }

            if (!Objects.equals(bo.getOrgLevel() - 1, OrgTypeEnum.getOrgLevelByOrgLevelCode(orgInfoPo.getOrgLevelCode()))) {
                throw new OrgInfoException(OrgInfoErrorCode.PARENT_ORG_CODE_ERROR);
            }

            OrgPathDo orgPathDo = baseMapper.selectPathByParentOrgCode(bo.getParentCode(), bo.getOrgLevel() - 1);
            StringBuilder fullCodePath = new StringBuilder();
            StringBuilder fullNamePath = new StringBuilder();

            switch (OrgTypeEnum.getEnumByOrgLevel(bo.getOrgLevel())) {
                case GROUP:
                    orgPathDo.setGroupCode(bo.getOrgCode());
                    orgPathDo.setGroupName(bo.getOrgName());
                    fullCodePath.append(orgPathDo.getPlatformCode()).append("-").append(orgPathDo.getGroupCode());
                    fullNamePath.append(orgPathDo.getPlatformName()).append("-").append(orgPathDo.getGroupName());
                    break;
                case MERCHANT:
                    orgPathDo.setMerchantCode(bo.getOrgCode());
                    orgPathDo.setMerchantName(bo.getOrgName());
                    fullCodePath.append(orgPathDo.getPlatformCode()).append("-").append(orgPathDo.getGroupCode()).append("-").append(orgPathDo.getMerchantCode());
                    fullNamePath.append(orgPathDo.getPlatformName()).append("-").append(orgPathDo.getGroupName()).append("-").append(orgPathDo.getMerchantName());
                    break;
                case STORE:
                    orgPathDo.setBuildCode(bo.getOrgCode());
                    orgPathDo.setStoreName(bo.getOrgName());
                    fullCodePath.append(orgPathDo.getPlatformCode()).append("-").append(orgPathDo.getGroupCode()).append("-").append(orgPathDo.getMerchantCode()).append("-").append(orgPathDo.getBuildCode());
                    fullNamePath.append(orgPathDo.getPlatformName()).append("-").append(orgPathDo.getGroupName()).append("-").append(orgPathDo.getMerchantName()).append("-").append(orgPathDo.getStoreName());
                default:
                    break;
            }

            orgInfo.setFullCodePath(fullCodePath.toString());
            orgInfo.setFullNamePath(fullNamePath.toString());
            orgInfo.setParentOrgCode(bo.getParentCode());
        } else {
            orgInfo.setFullCodePath(bo.getOrgCode());
            orgInfo.setFullNamePath(bo.getOrgName());
        }


        long id = IdUtil.createSnowflake(Constant.WORKER_ID, Constant.DATA_CENTER_ID).nextId();
        orgInfo.setId(id);
        // 平台 集团 id与code相同
        if (OrgTypeEnum.PLATFORM.getOrgLevel().equals(bo.getOrgLevel()) || OrgTypeEnum.GROUP.getOrgLevel().equals(bo.getOrgLevel())) {
            orgInfo.setOrgCode(String.valueOf(id));
        } else {
            orgInfo.setOrgCode(bo.getOrgCode());
        }
        orgInfo.setOrgName(bo.getOrgName());
        orgInfo.setCreateBy("yhb");
        orgInfo.setCreateDate(new Date());
        orgInfo.setIsDeleted(0);

        baseMapper.insert(orgInfo);
    }
}
